
import { TotpInfo } from './totp'

export class KeyManagement {
    path: string;
    saveKeys: (path: string, data: Array<TotpInfo>) => void;

    keys: TotpInfo[];
    constructor(path: string, 
        loadKeys: (path: string) => Array<TotpInfo>,
        saveKeys: (path: string, data: Array<TotpInfo>) => void
    ) {
        if (path) {
            this.path = path;
            this.keys = loadKeys(path);
            this.saveKeys = saveKeys;
        }

        if (!this.keys) {
            this.keys = new Array<TotpInfo>();
        }
    } 

    add(key: TotpInfo) {
        if (key) {
            this.keys.push(key);
        }

        if (this.path) {
            this.saveKeys(this.path, this.keys);
        }
    }

    del(index: number) {
        if (index < this.keys.length) {
            this.keys.splice(index, 1);
        }
    }

    get(index: number) {
        return this.keys[index];
    }

    size() {
        return this.keys.length;
    }
}
